package com.tvunetworks.device.extra.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.model.dto.GridDto;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.device.extra.annotation.Logined;
import com.tvunetworks.device.extra.model.param.GridDeviceParam;
import com.tvunetworks.device.extra.model.param.GridParam;
import com.tvunetworks.device.extra.service.GridService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * grid related
 * @author andy
 * @since 2022/3/23
 **/
@RestController
@RequestMapping("grid")
public class GridController {
    @Autowired
    private GridService gridService;

    /**
     * generator grid stream
     * @param gridParam param
     * @param loginUser login
     * @return obj
     * @throws Exception e
     */
    @PostMapping("/startLive")
    public Result<Object> startLive(@RequestBody GridParam gridParam, @Logined LoginUser loginUser) throws Exception{
        gridParam.setUserId(loginUser.getId());
       return gridService.start(gridParam);
    }

    /**
     * 配对并生成grid
     * @param gridParam param
     * @param loginUser login
     * @throws IOException e
     * @return obj
     */
    @PostMapping("/pair")
    public Result<Object> pair(@RequestBody GridParam gridParam,@Logined LoginUser loginUser) throws IOException {
        gridParam.setUserId(loginUser.getId());
        if(StringUtils.isEmpty(gridParam.getPairPid())){
            return ResultUtil.paramError("pairPd is empty");
        }
        return gridService.pair(gridParam);
    }

    /**
     * stop grid stream
     * @param gridParam param
     * @param loginUser login
     * @return obj
     * @throws Exception e
     */
    @RequestMapping("/stop")
    public Result stopLive(@RequestBody GridParam gridParam,@Logined LoginUser loginUser) throws Exception{
        gridParam.setUserId(loginUser.getId());
        if(StringUtils.isEmpty(gridParam.getTargetId())||StringUtils.isEmpty(gridParam.getRid())){
            return ResultUtil.paramError("targetId or rid is empty");
        }
        return gridService.stop(gridParam);
    }

    /**
     * get status
     * @param gridId gridId
     * @exception Exception e
     * @return result
     */
    @GetMapping("/get/status")
    public Result<Object> getGridStatus(@RequestParam String gridId) throws Exception{
        return gridService.getGridStatus(gridId);
    }

    /**
     * add or update
     * @param gridDto griddto
     * @param loginUser login
     * @return r
     */
    @PostMapping("/addOrUpdate")
    public Result<Object> addOrUpdateGrid(@RequestBody GridDto gridDto,@Logined LoginUser loginUser){
        gridDto.setUserId(loginUser.getId());
        if(StringUtils.isEmpty(gridDto.getTargetId())|| StringUtils.isEmpty(gridDto.getNickname())){
            return ResultUtil.paramError("targetId or nickname empty");
        }
        return gridService.addOrUpdateGrid(gridDto);
    }
    /**
     * get grid
     * @param rids r
     * @param condition condition
     * @return r
     */
    @GetMapping("/get")
    public Result getGrid(@RequestParam("rids")String rids,@RequestParam(required = false)String condition ){
        return gridService.getGridList(rids,condition);
    }

    /**
     * delete
     * @param rids rids
     * @param targetId targetId
     * @param pairId pairId
     * @return r
     */
    @DeleteMapping("delete")
    public Result delete(@RequestParam("rids") String rids,@RequestParam(required = false)String targetId,
                         @RequestParam(required = false)String pairId){
        return gridService.delete(rids,targetId,pairId);
    }

    /**
     * 获取用户所有R
     * @param gridDeviceParam  GridDeviceDto
     * @param loginUser LoginUser
     * @return r
     */
    @PostMapping("userDevice")
    public Result getUserDevice(@RequestBody GridDeviceParam gridDeviceParam, @Logined LoginUser loginUser){
        return gridService.getUserDevice(loginUser.getId(),gridDeviceParam);
    }



}
